using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DetailsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["RecruiterID"]==null)
            Response.Redirect("RecruitersPage.aspx");
    }
    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
        {
            SqlDataSource1.DeleteCommand=
            "Delete from tblRCAccounts where "+
            " RecruiterID=@RecruiterID";
            SqlDataSource1.DeleteParameters.Add("RecruiterID",
             Convert.ToString(DetailsView1.SelectedValue));
            if(SqlDataSource1.Delete()>0)
                Response.Redirect("RecruitersPage.aspx");
        }
 
    }
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        if (DetailsView1.CurrentMode== DetailsViewMode.Edit)
        {
            SqlDataSource1.UpdateCommand =
            "Update tblRCAccounts set Email=@ctl01,"
            + "CompanyName=@ctl02,"
            + "EnglishName=@ctl03,"
            + "OpenDate=@ctl04,"
            + "ContactName=@ctl05,"
            + "Address=@ctl06,ProvinceID=@ctl07,"            
            + "Phone=@ctl08,Fax=@ctl09, "  
            +" LastLogin=Getdate() " +
            " where RecruiterID=@RecruiterID";
            
             UpdateParameters();
            
            SqlDataSource1.UpdateParameters.Add("RecruiterID",
            Convert.ToString(DetailsView1.SelectedValue));            
            if (SqlDataSource1.Update() > 0)
                Response.Redirect("RecruitersPage.aspx");
        }
    }
    void UpdateParameters()
    {
        for(int i=1;i<10;i++)
        {
            
            SqlDataSource1.UpdateParameters.Add("ctl0"
            + i.ToString(), ((TextBox)DetailsView1.Rows[
                i].Cells[1].FindControl("ctl0" + i.ToString())).Text);
        }   
        
    }
    void InsertParameters()
    {
        for (int i = 2; i < 12; i++)
        {
            string str= "0" + i.ToString();
            SqlDataSource1.InsertParameters.Add("ctl"+
            str.Substring(str.Length-2), ((TextBox)DetailsView1.Rows[
                i-2].Cells[1].FindControl("ctl" + str.Substring(str.Length - 2))).Text);
        }
       
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
        {
            SqlDataSource1.InsertCommand =
            "Insert into tblRCAccounts(Email,"
            +"CompanyName,EnglishName,OpenDate"
            +", ContactName,Address ,ProvinceID"
            +",Phone,Fax ,LastLogin) "
            +" values(@ctl02,@ctl03,"
            + "@ctl04, @ctl05, @ctl06,"
            + "@ctl07,@ctl08,@ctl09,@ctl10,"
            + "@ctl11 )";

            InsertParameters();             
            if (SqlDataSource1.Insert() > 0)
                Response.Redirect("RecruitersPage.aspx");
        }
    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {

    }
}
